﻿//
// GNS_GameDown.cpp
// MobileManageSys
//FuncTion 游戏下载
// Created by mr.l on 16/1/14.
//
//

#include "GNS_GameDown.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_IOSHelper.h"
#include "GNS_PlayerMgr.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()

GNS_GameDown::GNS_GameDown()
{
    pGNS_GameDown=0;
    pMakeLayer=nullptr;
    ManageStr="";
    GameHallStr="";
}

GNS_GameDown::~GNS_GameDown()
{
    
}

GNS_GameDown* GNS_GameDown::createLayer()
{
    auto pGNS_GameDown = GNS_GameDown::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_GameDown);
    return pGNS_GameDown;
}

bool GNS_GameDown::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    return true;
}
void GNS_GameDown::SetNodeName(std::string CsbName)
{
    pGNS_GameDown = CSLoader::createNode(CsbName);
    pGNS_GameDown->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_GameDown);
    addChild(pGNS_GameDown,11);
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_GameDown);
    auto pBackBtn = static_cast<Button*>(pGNS_GameDown->getChildByName("GameDownBackBtn"));
    pBackBtn->setTag(10);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_GameDown::BtnCallBack, this));
    
    auto ManageCopy = static_cast<Button *>(pGNS_GameDown->getChildByName("Copy"));
    ManageCopy->setTag(1);
    ManageCopy->addTouchEventListener(CC_CALLBACK_2(GNS_GameDown::BtnCallBack, this));
    
    auto GameHallCopy = static_cast<Button *>(pGNS_GameDown->getChildByName("Copy1"));
    GameHallCopy->setTag(2);
    GameHallCopy->addTouchEventListener(CC_CALLBACK_2(GNS_GameDown::BtnCallBack, this));
    
    //加载遮罩
    pMakeLayer = GNS_MakeLayer::CreateMakeLayer("加载中...");
    this->addChild(pMakeLayer,999);
}

void GNS_GameDown::SetQRImagManage(std::string URLManage)
{
    bool bRet = m_QREncode.EncodeData(0, 0, 1, -1, (char*)URLManage.c_str());
    if(bRet)
    {
        int nSize = 10; // 定义好像素点的大小
        int originalSize = m_QREncode.m_nSymbleSize + (QR_MARGIN * 2);
        DrawNode *pQRNode = DrawNode::create();
        // 绘制像素点
        for (int i = 0; i < m_QREncode.m_nSymbleSize; ++i)
        {
            for (int j = 0; j < m_QREncode.m_nSymbleSize; ++j)
            {
                if (m_QREncode.m_byModuleData[i][j]==1)
                {
                    pQRNode->drawSolidRect(Vec2((i + QR_MARGIN)*nSize, (j + QR_MARGIN)*nSize), Vec2(((i + QR_MARGIN) + 1)*nSize, ((j + QR_MARGIN) + 1)*nSize), Color4F(0, 0, 0, 1));
                }
                else
                {
                    pQRNode->drawSolidRect(Vec2((i + QR_MARGIN)*nSize, (j + QR_MARGIN)*nSize), Vec2(((i + QR_MARGIN) + 1)*nSize, ((j + QR_MARGIN) + 1)*nSize), Color4F(1, 1, 1, 1));
                }
            }
        }
        // 绘制外框
        pQRNode->drawSolidRect(Vec2(0, 0), Vec2((m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize, (QR_MARGIN)*nSize), Color4F(1, 1, 1, 1));
        pQRNode->drawSolidRect(Vec2(0, 0), Vec2((QR_MARGIN)*nSize, (m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize), Color4F(1, 1, 1, 1));
        pQRNode->drawSolidRect(Vec2((m_QREncode.m_nSymbleSize + QR_MARGIN)*nSize, 0), Vec2((m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize, (m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize), Color4F(1, 1, 1, 1));
        pQRNode->drawSolidRect(Vec2(0, (m_QREncode.m_nSymbleSize + QR_MARGIN)*nSize), Vec2((m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize, (m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize), Color4F(1, 1, 1, 1));
        Size winSize = Director::getInstance()->getWinSize();
        pQRNode->setPosition(Vec2((winSize.width - nSize*m_QREncode.m_nSymbleSize) / 2, winSize.height - (winSize.height - nSize*m_QREncode.m_nSymbleSize) / 2+240));
        pQRNode->setContentSize(Size(155,130));
        pQRNode->setScaleY(-1);
       pGNS_GameDown->addChild(pQRNode);
    }

}

void GNS_GameDown::SetQRImagGameHall(std::string URLManage)
{
    bool bRet = m_QREncode.EncodeData(0, 0, 1, -1, (char*)URLManage.c_str());
    if(bRet)
    {
        int nSize = 10; // 定义好像素点的大小
        int originalSize = m_QREncode.m_nSymbleSize + (QR_MARGIN * 2);
        DrawNode *pQRNode = DrawNode::create();
        // 绘制像素点
        for (int i = 0; i < m_QREncode.m_nSymbleSize; ++i)
        {
            for (int j = 0; j < m_QREncode.m_nSymbleSize; ++j)
            {
                if (m_QREncode.m_byModuleData[i][j]==1)
                {
                    pQRNode->drawSolidRect(Vec2((i + QR_MARGIN)*nSize, (j + QR_MARGIN)*nSize), Vec2(((i + QR_MARGIN) + 1)*nSize, ((j + QR_MARGIN) + 1)*nSize), Color4F(0, 0, 0, 1));
                }
                else
                {
                    pQRNode->drawSolidRect(Vec2((i + QR_MARGIN)*nSize, (j + QR_MARGIN)*nSize), Vec2(((i + QR_MARGIN) + 1)*nSize, ((j + QR_MARGIN) + 1)*nSize), Color4F(1, 1, 1, 1));
                }
            }
        }
        // 绘制外框
        pQRNode->drawSolidRect(Vec2(0, 0), Vec2((m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize, (QR_MARGIN)*nSize), Color4F(1, 1, 1, 1));
        pQRNode->drawSolidRect(Vec2(0, 0), Vec2((QR_MARGIN)*nSize, (m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize), Color4F(1, 1, 1, 1));
        pQRNode->drawSolidRect(Vec2((m_QREncode.m_nSymbleSize + QR_MARGIN)*nSize, 0), Vec2((m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize, (m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize), Color4F(1, 1, 1, 1));
        pQRNode->drawSolidRect(Vec2(0, (m_QREncode.m_nSymbleSize + QR_MARGIN)*nSize), Vec2((m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize, (m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize), Color4F(1, 1, 1, 1));
        Size winSize = Director::getInstance()->getWinSize();
        pQRNode->setPosition(Vec2((winSize.width - nSize*m_QREncode.m_nSymbleSize) / 2, winSize.height - (winSize.height - nSize*m_QREncode.m_nSymbleSize) / 2-220));
        pQRNode->setContentSize(Size(155,130));
        pQRNode->setScaleY(-1);
        pGNS_GameDown->addChild(pQRNode);
    }
    

}
void GNS_GameDown::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 10:
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.clear();
                    this->removeAllChildrenWithCleanup(true);
                    this->getParent()->removeChild(this);
                }
                //auto SystemNode=GNS_PlayerMgr::getInstance()->_NodeHallVec.at(0);
                //SystemNode->setVisible(true);

				GNS_NodeMgr::getInstance()->setMenuVisible("SystemMgr", true);
                break;
            }
            case 1:
            {
#if (CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
                bool IsSuccessful=false;
                if (ManageStr.length()!=0) {
                    IsSuccessful=GNS_IOSHelper::GetInstance()->GetClipboard(ManageStr);
                    if (IsSuccessful==true) {
                        auto PopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
                        PopWnd->SetTitleWithUTF("提示!");
                        PopWnd->SetContentTextWithUTF("复制成功!",Color3B(0,0,0),28);
                    } else{
                        auto PopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
                        PopWnd->SetTitleWithUTF("提示!");
                        PopWnd->SetContentTextWithUTF("复制失败!",Color3B(0,0,0),28);
                    }
                }
#else
                
#endif


                break;
            }
            case 2:
            {
#if (CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
                bool IsSuccessful=false;
                if (GameHallStr.length()!=0) {
                    IsSuccessful=GNS_IOSHelper::GetInstance()->GetClipboard(GameHallStr);
                    if (IsSuccessful==true) {
                        auto PopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
                        PopWnd->SetTitleWithUTF("提示!");
                        PopWnd->SetContentTextWithUTF("复制成功!",Color3B(0,0,0),28);
                    } else{
                        auto PopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
                        PopWnd->SetTitleWithUTF("提示!");
                        PopWnd->SetContentTextWithUTF("复制失败!",Color3B(0,0,0),28);
                    }
                }
#else
                
#endif
                break;
            }
            default:
                break;
        }
    }
}
void GNS_GameDown::ShowQRImage()
{
    pMakeLayer->Show(false);
    for (auto it:*GNS_PlayerMgr::getInstance()->getGameDown()) {
        if (it.iGameID==104) {
            ManageStr=it.szResText;
            SetQRImagManage(ManageStr);
        }else if(it.iGameID==102){
            GameHallStr=it.szResText;
            SetQRImagGameHall(GameHallStr);
        }
    }
    
    
}